package com.lenovo.leos.filebrowser.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.MimeTypeParser;
import com.lenovo.leos.filebrowser.engine.util.MimeTypes;

public class FileSearchAdapter extends BaseAdapter {
	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private LayoutInflater mInflater;

	private List<IconifiedText> mItems = null;
	private static final String TAG = "FileSearchAdapter";
	boolean flag = false;
	int checkedPos;
	boolean mChecked = false;
	private static final String ZIP_MIMETYPE = "compressor/zip";
	String fileName;

	public FileSearchAdapter(Context context, List<IconifiedText> mItems) {
		Log.d(TAG, "mItems=====================" + mItems);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		this.mItems = new ArrayList<IconifiedText>();

		if (mItems != null)
			this.mItems.addAll(mItems);
	}

	public void setListItems(List<IconifiedText> lit) {
		mItems.clear();

		if (lit != null)
			mItems.addAll(lit);
	}

	/** @return The number of items in the */
	public int getCount() {
		return mItems.size();
	}

	public Object getItem(int position) {
		return mItems.get(position);
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	HashMap<String, Drawable> mMimeIconMap;

	public Drawable getDrawableForMimetype(String mimetype) {
		Drawable icon = null;

		if (mMimeIconMap == null) {
			mMimeIconMap = new HashMap<String, Drawable>();
		}

		icon = mMimeIconMap.get(mimetype);
		if (icon == null) {
			PackageManager pm = FileSearcher.getInstance().getPackageManager();
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(Uri.fromParts("file", "", null), mimetype);

			List<ResolveInfo> lri = pm.queryIntentActivities(intent,
					PackageManager.MATCH_DEFAULT_ONLY);
			List<ResolveInfo> lriRe = pm.queryBroadcastReceivers(intent,
					PackageManager.MATCH_DEFAULT_ONLY);
			if (lri != null && lri.size() > 0) {
				icon = lri.get(0).loadIcon(pm);
			} else if (lriRe != null && lriRe.size() > 0) {
				icon = lriRe.get(0).loadIcon(pm);
			}

			Log.d(TAG, "@@@@@@@@@@@@@@@@@@@@ mimetype = " + mimetype.toString());
			if (icon != null) {
				mMimeIconMap.put(mimetype, icon);
			} else if (mimetype.equals(ZIP_MIMETYPE)) { // add 2011-01-12 by xqm
				icon = FileSearcher.getInstance().getResources()
						.getDrawable(R.drawable.zip);
				Log.d(TAG, "@@@@@@@@@@@@@@@@" + icon.toString());
				mMimeIconMap.put(mimetype, icon);
			} else {
				icon = FileSearcher.getInstance().getResources()
						.getDrawable(R.drawable.icon_unknown_file);
				mMimeIconMap.put(mimetype, icon);
			}
		}
		return icon;
	}

	/**
	 * @param convertView
	 *            The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an IconifiedText
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is
		// no need// to reinflate it. We only inflate a new View when the
		// convertView// supplied// by ListView is null.
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.filesearchlistitem, null);

			// Creates a ViewHolder and store references to the two children//
			// views// we want to bind data to.
			holder = new ViewHolder();
			holder.item_name = (TextView) convertView
					.findViewById(R.id.item_name);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.item_data = (TextView) convertView
					.findViewById(R.id.item_data);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		fileName = mItems.get(position).getFile().getName();
		String s;
		if (mItems.get(position).getFile().isDirectory()) {
			s = mItems.get(position).getFile().getAbsolutePath();
		} else {
			s = mItems.get(position).getFile().getParent();
		}
		
		if(FileSearcher.cut_copy && FileSearcher.opType == FileSearcher.OPTYPE.CUT && mItems.get(position).getIsCut()){
			holder.item_name.setTextColor(Color.RED);//Color.DKGRAY
		}else{
			holder.item_name.setTextColor(Color.WHITE);
		}
		
		holder.item_data.setText(s);
		holder.item_name.setText(fileName);
		holder.icon.setAdjustViewBounds(true);
		holder.icon.setImageDrawable(mItems.get(position).getIcon());
		return convertView;
	}

	public class ViewHolder {
		TextView item_name;
		ImageView icon;
		TextView item_data;
	}
}
